package com.skycode.core.configuration;


import com.skycode.core.manager.RecursosManager;
import java.sql.SQLException;
import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class WebServletContextListener implements ServletContextListener {

    private static WebApplicationContext springContext;
    

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        springContext = null;
    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            springContext = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
            
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.getViewRoot().setLocale(new Locale("es_PE"));
            fc.getApplication().setDefaultLocale(FacesContext.getCurrentInstance().getViewRoot().getLocale());                
            System.out.println("Entrando ....");
            RecursosManager recursosManager = (RecursosManager) getApplicationContext().getBean("recursosManager");
            recursosManager.cargarInformacionBaseDatos();
            //ApplicationHelper.cargarParametrosGenerales();
            //ApplicationHelper.cargarParametrosGenerales();
        } catch (NamingException ex) {
            
        } catch (SQLException ex) {
            
        }
    }

    public static ApplicationContext getApplicationContext() {
        return springContext;
    }
}
